<?php
class FriendAction extends Action{
	
	public function searchFriend(){
		$uid = $_REQUEST['uid'];
		$phone = $_REQUEST['phone'];
		//echo 'phone'.$phone;exit;
		if(empty($phone) || empty($uid)){//参数不能为空
			$value = ['code'=>'empty'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		//echo $phone;exit;
		$user = M('User');
		$result = $user->where("login = $phone")->field("uid,uname,sex,intro,phone,brith,avatar,is_coach,province,city")->find();
		$fid = $result['uid'];
		if(!$result){//查无此人
			$value = ['code'=>'nobody'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		if($uid == $fid){//自己
			$value = ['code'=>'myself'];
			header("Content-Type: text/json");
			die(json_encode($value));
		
		}
		$follow = M('user_follow');
		$is_friend= $follow->where("uid = '$uid' and fid = '$fid' and is_friend = 1")->find();
		if($is_friend){
			$result['is_friend'] = 1;
		}else{
			$result['is_friend'] = 0;
		}
		$value = ['code'=>'success','data'=>$result];
		header("Content-Type: text/json");
		die(json_encode($value));
		
	}
	
	public function addFriend($uid,$fid){//【添加好友】
		$uid = $_POST['uid'];
		$fid = $_POST['fid'];
		
		$is_follow = $this->isFollow($uid, $fid);
		$is_followed = $this->isFollow($fid, $uid);
		
		if(!$is_follow){//未关注
			$this->follow($uid, $fid);
		}
		if(!$is_followed){//未被关注
			$this->follow($fid, $uid);
		}
		
		$add_friend = $this->doAddFriend($uid, $fid);
		if($add_friend){
			$value = ['code'=>'success'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		
	}
	
	public function getFriend(){//【获取好友列表】
		$uid = $_REQUEST['uid'];
		$follow = M('User_follow');
		$result = $follow->join("yjuser U on yjuser_follow.fid = U.uid")->where("yjuser_follow.uid = '$uid' and yjuser_follow.is_friend = 1")->field("yjuser_follow.fid as uid,U.uname as uname,U.avatar as avatar")->order("convert(U.uname using gb2312) asc")->select();
		
		if($result){
			
			foreach ($result as $key=>$v){
			$name = mb_convert_encoding($v['uname'],"gb2312","auto");
			$result[$key]['flag'] = $this->getinitial($name);
			}
			$value = ['code'=>'success','data'=>$result];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			if(is_array($result) || mysql_num_rows($result) < 1){
				$value = ['code'=>'success','data'=>array()];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}	
		
	}
	
	public function deleteFriend(){
		$uid = $_REQUEST['uid'];
		$fid = $_REQUEST['fid'];
		$friend = M('User_follow');
		$find = $friend->where("uid = $uid and fid = $fid and is_friend = 1")->find();
		if(!$find){//对方不是好友
			$value = ['code'=>'notfound'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		$delete = $friend->where("(uid = $uid and fid = $fid) or (uid = $fid and fid = $uid)")->delete();
		$del_follow = M("User_follow_apply")->where("uid = $uid and fid = $fid")->delete();
		$del_follow2 = M("User_follow_apply")->where("uid = $fid and fid = $uid")->delete();
		
		if($delete){
			$value = ['code'=>'success'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}

	private function follow($uid1,$uid2){
		$uid = $uid1;
		$fid = $uid2;
		$follow = model('Follow');
		$result = $follow->doFollow($uid,$fid);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	private function isFollow($uid1,$uid2){
	
		$uid = $uid1;
		$fid = $uid2;
		$follow = M('user_follow');
		$result = $follow->where("uid = '$uid' and fid = '$fid'")->find();
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	private function doAddFriend($uid,$fid){
		$is_follow = $this->isFollow($uid, $fid);
		$is_followed = $this->isFollow($fid, $uid);
		$follow = M('User_follow');
		if($is_follow && $is_followed){//已相互关注
			$data['is_friend'] = 1;
			$result = $follow->where("(uid = '$uid' and fid = '$fid') or (uid = '$fid' and fid = '$uid')")->data($data)->save();
			if($result){
				return true;
			}else{
				return false;
			}
		}else{//未相互关注
			return false;
		}
	}
	
	
	private function getinitial($str){
	$asc=ord(substr($str,0,1));
	if ($asc<160) //非中文
	{
	if ($asc>=48 && $asc<=57){
	return substr($str,0,1); //数字
	}elseif ($asc>=65 && $asc<=90){
	return chr($asc); // A--Z
	}elseif ($asc>=97 && $asc<=122){
	return chr($asc-32); // a--z
	}else{
	return '~'; //其他
	}
	}
	else //中文
	{
	$asc=$asc*1000+ord(substr($str,1,1));
	//获取拼音首字母A--Z
	if ($asc>=176161 && $asc<176197){
	return 'A';
	}elseif ($asc>=176197 && $asc<178193){
	return 'B';
	}elseif ($asc>=178193 && $asc<180238){
	return 'C';
	}elseif ($asc>=180238 && $asc<182234){
	return 'D';
	}elseif ($asc>=182234 && $asc<183162){
	return 'E';
	}elseif ($asc>=183162 && $asc<184193){
	return 'F';
	}elseif ($asc>=184193 && $asc<185254){
	return 'G';
	}elseif ($asc>=185254 && $asc<187247){
	return 'H';
	}elseif ($asc>=187247 && $asc<191166){
	return 'J';
	}elseif ($asc>=191166 && $asc<192172){
	return 'K';
	}elseif ($asc>=192172 && $asc<194232){
	return 'L';
	}elseif ($asc>=194232 && $asc<196195){
	return 'M';
	}elseif ($asc>=196195 && $asc<197182){
	return 'N';
	}elseif ($asc>=197182 && $asc<197190){
	return 'O';
	}elseif ($asc>=197190 && $asc<198218){
	return 'P';
	}elseif ($asc>=198218 && $asc<200187){
	return 'Q';
	}elseif ($asc>=200187 && $asc<200246){
	return 'R';
	}elseif ($asc>=200246 && $asc<203250){
	return 'S';
	}elseif ($asc>=203250 && $asc<205218){
	return 'T';
	}elseif ($asc>=205218 && $asc<206244){
	return 'W';
	}elseif ($asc>=206244 && $asc<209185){
	return 'X';
	}elseif ($asc>=209185 && $asc<212209){
	return 'Y';
	}elseif ($asc>=212209){
	return 'Z';
	}else{
	return '~';
	}
	}
		
		
	}
	
	
	
	
	
	
	
	
}